package com.stkent.polygondrawingutildemo.slice;

import com.stkent.polygondrawingutil.PolygonDrawingUtil;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;

import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * 自定义控件DemoComponent
 *
 * @since : 2021.5.24
 */
public class DemoView extends Component implements Component.DrawTask {
    /**
     * 边数最小为3
     */
    private  Integer numberOfSides = 3;
    /**
     * 内角最大为120
     */
    private  Float cornerRadius = 120f;
    /**
     * 旋转度数默认为0
     */
    private  Float polygonRotation = 0f;
    /**
     * 缩放默认为0
     */
    private  Float scale = 0f;

    private PolygonDrawingUtil polygonDrawingUtil;
    private Paint polygonFillPaint;
    private Paint polygonStrokePaint;
    private Path strokePath;
    private Float strokeWidth = 0f;

    /**
     * 构造器
     *
     * @param context context
     * @param attrSet 属性
     */
    public DemoView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init();
        addDrawTask(this);
    }

    /**
     * 获取边数
     *
     * @return numberOfSides 边数
     */
    public Integer getNumberOfSides() {
        return numberOfSides;
    }

    /**
     * 获取角度
     *
     * @return cornerRadius 角度
     */
    public Float getCornerRadius() {
        return cornerRadius;
    }

    /**
     * 获取旋转角度
     *
     * @return polygonRotation 旋转角度
     */
    public Float getPolygonRotation() {
        return polygonRotation;
    }


    private void init() {
        strokePath = new Path();
        polygonDrawingUtil = new PolygonDrawingUtil();
        polygonFillPaint = new Paint();
        polygonFillPaint.setAntiAlias(true);
        polygonStrokePaint = new Paint();
        polygonStrokePaint.setAntiAlias(true);
        strokeWidth = 8.0f;
        Color polygonFillPaintColor = new Color(Color.getIntColor("#66FF4081"));
        polygonFillPaint.setColor(polygonFillPaintColor);
        polygonFillPaint.setStyle(Paint.Style.FILL_STYLE);
        Color polygonStrokePaintColor = new Color(Color.getIntColor("#FF4081"));
        polygonStrokePaint.setColor(polygonStrokePaintColor);
        polygonStrokePaint.setStyle(Paint.Style.STROKE_STYLE);
        polygonStrokePaint.setStrokeWidth(strokeWidth);
    }

    /**
     * 设置边数
     *
     * @param numberOfSides 边数
     */
    public void setNumberOfSides(Integer numberOfSides) {
        this.numberOfSides = numberOfSides;
        invalidate();
    }

    /**
     * 设置内角度数
     *
     * @param cornerRadius 度数
     */
    public void setCornerRadius(Float cornerRadius) {
        this.cornerRadius = cornerRadius;
        invalidate();
    }

    /**
     * 设置旋转的度数
     *
     * @param polygonRotation 度数
     */
    public void setPolygonRotation(Float polygonRotation) {
        this.polygonRotation = polygonRotation;
        invalidate();
    }

    /**
     * 设置多边形缩放的比例
     *
     * @param scale 比例
     */
    public void setScale(Float scale) {
        this.scale = scale;
        invalidate();
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        Float centerX = (component.getWidth() / 2f);
        Float centerY = (component.getHeight() / 2f);
        Float radius = (float) ((double) scale * ((double) (component.getWidth()) / 2f - (double) strokeWidth));
        // Method 1 (simpler for direct drawing):
        polygonDrawingUtil.drawPolygon(
                canvas,
                this.numberOfSides,
                centerX,
                centerY,
                radius,
                cornerRadius,
                polygonRotation,
                polygonFillPaint
        );
        // Method 2 (allows polygon Path post-processing if desired):
        polygonDrawingUtil.constructPolygonPath(
                strokePath,
                this.numberOfSides,
                centerX,
                centerY,
                radius,
                cornerRadius,
                polygonRotation
        );
        canvas.drawPath(strokePath, polygonStrokePaint);
    }
}
